import {
  getVoucherList
} from "../../utils/config"
import store from "../../utils/factory"
let that = null;
const util = require('../../utils/util.js')

Page({
  data: {
    showtab: 0, //顶部选项卡索引
    limit: 10,
    page: 1,
    noData: false,
    finish: false,
    status: "未使用",
    mode: "normal",
    submitStatus: false,
    total: 0,
    tabnav: {
      tabnum: 5,
      tabitem: [{
          "id": 0,
          "text": "未使用"
        },
        {
          "id": 1,
          "text": "已使用"
        },
        {
          "id": 2,
          "text": "已过期"
        }
      ]
    },
    productList: [],
  },
  onLoad: function (e) {
    that = this;
    if (e && e.type == "choice") {
      this.setData({
        mode: "choice",
        total: e.total
      })
    }
  },
  onShow() {
    that = this;
    this.initPageStatus();
  },
  initsubmitStatus() {
    that.setData({
      submitStatus: false
    })
  },
  choose(e) {
    let id = e.currentTarget.dataset.id;
    let value = e.currentTarget.dataset.value
    if (this.data.mode == "normal") {
      return false
    }
    store.set("discount", {
      id: id,
      value: value
    });
    // 跳转到页面 A
    wx.navigateBack({
      delta: 1
    });
  },
  initPageStatus() {
    this.setData({
      finish: false,
      page: 1,
      noData: false,
      submitStatus: false,
      productList: []
    })
    this.getVoucherList();
  },
  setTab: function (e) {
    const edata = e.currentTarget.dataset;
    if (this.data.showtab == edata.tabindex) {
      return false
    }
    this.setData({
      showtab: edata.tabindex,
      productList: [],
      page: 1,
      noData: false,
      status: edata.status == "全部" ? "" : edata.status
    })
    this.getVoucherList()
  },
  getVoucherList() {
    getVoucherList({
      page: this.data.page,
      limit: 10,
      status: this.data.status,
      threshold: this.data.total || ""
    }).then(res => {
      if (res.data.length == 0) {
        this.setData({
          finish: true,
          noData: true
        })
        return false
      }
      if (res.data.length < 10) {
        this.setData({
          finish: true
        })
      }
      this.setData({
        productList: [...res.data, ...this.data.productList],
      })
    })
  },
  onReachBottom() {
    console.log("bottom")
    if (this.data.finish) {
      return false
    }
    this.setData({
      page: this.data.page + 1
    })
    this.getVoucherList()
  }
})